<?php
namespace backend\controllers;

use backend\models\PermissionModel;
use backend\models\AdminRoleModel;
use backend\models\AdminRolePermissionModel;
use backend\models\OrganizationModel;
use Yii;
use yii\data\Pagination;
use backend\base\BaseController;
use backend\models\AdminModel;

/**
 * Admin controller
 */
class AdminRolePermissionController extends BaseController
{
    /*
     * 角色列表
     */
    public function actionRoleList()
    {
        $data = parent::parseData($this->body);
        //查询
        $query = AdminRoleModel::find();
        self::orgSelect($query);
        $count = $query->count();
        //分页
        $pagination = new Pagination(['totalCount' => $count]);
        //列表
        $list = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->orderBy('id asc')
            ->all();
        return $this->render('role-list',['list' => $list,'pagination'=>$pagination]);

    }

    /**
     * 角色新增
     */
    public function actionRoleAdd()
    {
        $model = new AdminRoleModel(['scenario' => 'add']);
        $org = OrganizationModel::orgAll();
        if(Yii::$app->request->isPost){
            $data = parent::parseData($this->body);
            if ($model->load($data) && $model->validate() && $model->save()) {
                $this->addRecord('新增角色',$model->name);
                return $this->saveBack(['role-list']);
            }
        }
        return $this->render('role-add',['model' => $model,'org'=>$org]);
    }

    /**
     * 角色编辑
     */
    public function actionRoleEdit($id=0)
    {
        $model = AdminRoleModel::findOne($id);
        if(!$model){
            return $this->saveBack(['role-list']);
        }
        $model->setScenario('edit');
        $org = OrganizationModel::orgAll();
        if(Yii::$app->request->isPost){
            $data = parent::parseData($this->body);
            if ($model->load($data) && $model->validate() && $model->save()) {
                $this->addRecord('编辑角色',$model->name);
                return $this->saveBack(['role-list']);
            }
        }
        return $this->render('role-edit',array('model' => $model,'org'=>$org));
    }

    /*
     * 角色禁用|恢复
     */
    public function actionRoleStatusAjaxDo()
    {
        $data = parent::parseData($this->body);
        $model = AdminRoleModel::findOne($data['id']);
        if($model){
            $model->status = $data['status'] ? 1:0;
            if($model->save()){
                $this->addRecord('角色禁用|恢复',$model->name);
                return self::ReturnData('operate_success');
            }
        }
        return self::ReturnData('edit_error');
    }

    /*
     * 权限列表
     */
    public function actionPermissionList()
    {
        $keywords = Yii::$app->request->get('keywords','');
        $parent_id = Yii::$app->request->get('parent_id','');
        //查询
        $query = PermissionModel::find();
        if(!empty($keywords)){
            $query->andWhere(['or',['like','name',$keywords],['like','btn_code',$keywords],['like','menu_code',$keywords]]);
        }
        $query->andFilterWhere(['parent_id'=>$parent_id]);
        $count = $query->count();
        //分页
        $pagination = new Pagination(['totalCount' => $count]);
        //列表
        $list = $query->offset($pagination->offset)
            ->limit($pagination->limit)
            ->orderBy('id desc')
            ->all();
        $parent_rower = PermissionModel::AllMenu();
        return $this->render('permission-list',['list' => $list,'pagination'=>$pagination,'parent_rower'=>$parent_rower,'keywords'=>$keywords,'parent_id'=>$parent_id]);

    }

    /**
     * 权限新增
     */
    public function actionPermissionAdd()
    {
        $model = new PermissionModel(['scenario' => 'add']);
        if(Yii::$app->request->isPost){
            $data = parent::parseData($this->body);
            if ($model->load($data) && $model->validate() && $model->save()) {
                $this->addRecord('新增权限',$model->id);
                return $this->saveBack(['permission-list']);
            }
        }
        $parent_rower = PermissionModel::getParentList();
        return $this->render('permission-add',array('model' => $model,'parent_rower' => $parent_rower));
    }

    /**
     * 权限编辑
     */
    public function actionPermissionEdit($id=0)
    {
        $model = PermissionModel::findOne($id);
        if(!$model){
            return $this->saveBack(['permission-list']);
        }
        $model->setScenario('edit');
        if(Yii::$app->request->isPost){
            $data = parent::parseData($this->body);
            if ($model->load($data) && $model->validate() && $model->save()) {
                $this->addRecord('编辑权限',$id);
                return $this->saveBack(['permission-list']);
            }
        }
        $class_list = PermissionModel::levelList($model->id,$model->parent_id);
        if(count($class_list) >= 2) {
            unset($class_list[count($class_list) - 1]);
        }
        return $this->render('permission-edit',array('model' => $model,'class_list' => $class_list));
    }

    /*
     * 查找子菜单
     */
    public function actionMenuList()
    {
        if(Yii::$app->request->isPost){
            $data = parent::parseData($this->body);
            $parent_rower = PermissionModel::getParentList(false,$data['id']);
            if(!empty($parent_rower)){//返回保存成功信息
                return self::ReturnData('request_success',$parent_rower);
            }
            return parent::ajaxCustom();//返回参数验证失败信息
        }
    }

    /*
     * 权限禁用|恢复
     */
    public function actionPermissionStatusAjaxDo()
    {
        $data = parent::parseData($this->body);
        $model = PermissionModel::findOne($data['id']);
        if($model){
            $model->status = $data['status'] ? 1:0;
            if($model->save()){
                $this->addRecord('权限禁用|恢复',$data['id']);
                return self::ReturnData('operate_success');
            }
        }
        return self::ReturnData('edit_error');
    }


    /*
     * 分配权限
     */
    public function actionRolePermissionEdit($id=0)
    {
        $list = AdminRolePermissionModel::getRolePermissionList($id);
        return $this->render('role-permission-edit',['list' => $list, 'id'=>$id]);
    }

    /*
     * 分配权限保存
     */
    public function actionRolePermissionEditAjaxDo()
    {
        $data = Yii::$app->request->post();
        $role_id = isset($data['role_id']) ? (int)$data['role_id'] : 0;
        $ids = isset($data['id']) ? (array)$data['id'] : [];
        if($role_id){
            $model = AdminRolePermissionModel::findOne(['role_id'=>$role_id,'status'=>1]);
            if(empty($model)){
                $model = new AdminRolePermissionModel();
            }
            $model->permission_codes = implode(',',$ids);
            $model->role_id = $role_id;
            if($model->save()){
                $this->addRecord('分配权限',$role_id);
                return self::ReturnData('operate_success');
            }
        }
        return self::ReturnData('edit_error');
    }
}
